package com.bxd.baodan.fragment;

import android.content.Context;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import com.bxd.baodan.R;
import com.bxd.baodan.item.AdviserSearchConditionItem.DataItemX.RegionItem;
import com.bxd.baodan.item.model.CityData;
import com.bxd.baodan.item.model.ProvinceData;

import java.util.ArrayList;
import java.util.List;

public class AreaAction {

    private ListView lvPro;
    private ListView lvCity;


    private List<ProvinceData> proData;
    private List<CityData> cityData, cityCheckData;   //前者放置所有的城市数据，后者放置根据条件选择的城市数据

    private List<String> proDatas;
    private List<String> cityDatas;

    private List<ProvinceData> proLocal;
    private List<CityData> cityLocal;

    private List<RegionItem> list;


    private ArrayAdapter<String> cityAdapter, disAdapter;

    public AreaCheckListener getListener() {
        return listener;
    }

    public void setListener(AreaCheckListener listener) {
        this.listener = listener;
    }

    private AreaCheckListener listener;


    private Context ct;
    private View view;

    public AreaAction(Context ct, View view, List<RegionItem> list) {
        this.ct = ct;
        this.view = view;
        this.list = list;


        proLocal = new ArrayList<ProvinceData>();
        cityLocal = new ArrayList<CityData>();
        for (int i = 0; i < list.size(); i++) {
            // list.get(i).data
            ProvinceData pro = new ProvinceData();
            pro.setProID(Integer.parseInt(list.get(i).id));
            pro.setName(list.get(i).name);


            for (int j = 0; j < list.get(i).data.size(); j++) {
                CityData city = new CityData();
                city.setCityID(Integer.parseInt(list.get(i).data.get(j).id));
                city.setName(list.get(i).data.get(j).name);

                // FIXME: 17/7/25 修复city不显示的问题,原因是两者没有关联
                city.setProID(Integer.parseInt(list.get(i).id));
                cityLocal.add(city);


            }

            proLocal.add(pro);
        }
    }


    public void setData(final AreaCheckListener listener) {

        lvPro = (ListView) view.findViewById(R.id.lv_pro);
        lvCity = (ListView) view.findViewById(R.id.lv_city);


        proData = proLocal;
        proDatas = new ArrayList<>();
        for (ProvinceData data : proData) {
            proDatas.add(data.getName());
        }
        lvPro.setAdapter(new ArrayAdapter<>(ct, R.layout.area_text_item, proDatas));


        //初始化另外两个ListView
        cityData = new ArrayList<>();
        cityDatas = new ArrayList<>();
        cityCheckData = new ArrayList<>();
        cityAdapter = new ArrayAdapter<>(ct, R.layout.area_text_item, cityDatas);
        lvCity.setAdapter(cityAdapter);


        lvPro.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                cityData.clear();//添加数据前，先删除旧数据
                cityDatas.clear();
                cityCheckData.clear();


                int proId = proData.get(position).getProID();    //proId

                cityData.addAll(cityLocal);
                for (CityData data : cityData) {
                    if (data.getProID() == proId) {      //根据proId，查找名下的城市
                        cityDatas.add(data.getName());
                        cityCheckData.add(data);
                    }
                }
                cityAdapter.notifyDataSetChanged();    //通知listView更新数据
            }
        });

        lvCity.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                listener.checkedData(cityCheckData.get(position).getName(),
                        cityCheckData.get(position).getCityID() + "",
                        cityCheckData.get(position).getProID() + ""
                );

            }
        });
    }

    public void clearData() {
        cityData.clear();//添加数据前，先删除旧数据
        cityDatas.clear();
        cityCheckData.clear();
        cityAdapter.notifyDataSetChanged();
    }


    public interface AreaCheckListener {
        void checkedData(String city_name, String city_id, String pro_id);
    }


}

